package com.itheima.leetcode;

/**
 * 找出字符串中第一个匹配项的下标 - 双指针
 */
public class StrStrLeetcode28 {

    static int strStr(String haystack, String needle) {
        char[] origin = haystack.toCharArray(); // 原始
        char[] pattern = needle.toCharArray();  // 模式
        int i = 0;
        int j = 0;
        while (origin.length - pattern.length >= i) { // origin.length >= i + pattern.length
            for (j = 0; j < pattern.length; j++) {
                if (pattern[j] != origin[i + j]) {
                    break;
                }
            }
            if (pattern.length == j) {
                return i;
            }
            i++;
        }
        return -1;
    }

    public static void main(String[] args) {
        System.out.println(strStr("sadbutsad", "sad"));
    }
}
